<?php
/**
 * IGUAN licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc_ (http://www_qinqinbaby_com)
 */

class esb_rpc_request_taobao_api{

	private $c ;
	private  $top_session='';
	private  $seller_nick='';
	function __construct($params){

	 
		
		$this->c = new esb_rpc_request_taobao_top_TopClient;

		
		$node_id = $_POST['to_node_id'];
		$node_type = 'taobao';
		kernel::log("tonodeid:".$node_id);
		//根据shop_id查询session和nick_name
		$sql = sprintf('SELECT addon FROM sdb_ome_shop WHERE node_type="%s" AND node_id="%s" ',$node_type,$node_id);
		$addon = kernel::database()->select($sql);
		if($addon) {
			$addon = unserialize($addon[0]['addon']);
		}else{
			die('Shop id error.');
		}
		
		$this->top_session = $addon['session'];
		$this->seller_nick = $addon['nickname'];

	}

	/*
	 * 自己联系物流（线下物流）发货
	*/
	function store_logistics_offline_send ($params){

		$tid =$params['tid'];
		$outSid =$params['logistics_no'];//运单号
		$company_code = $params['logistics_code'] ;//物流公司代码.如"POST"就代表中国邮政 在缓存里查

		//检查订单发货状态，如果已发货则取消
// 		$req=new esb_rpc_requesttaobao_top_request_TradeGetRequest();
// 		$req = new TradeGetRequest;
// 		$req->setFields("status");
// 		$req->setTid($tid);

// 		$resp = $this->c->execute($req,$this->top_session);
		
// 		WAIT_SELLER_SEND_GOODS
		
		$req = new esb_rpc_request_taobao_top_request_LogisticsOfflineSendRequest();

		$req->setTid($tid);
		$req->setCompanyCode($company_code);
		$req->setOutSid($outSid);
			
		$resp = $this->c->execute($req,$this->top_session);// 执行API请求并打印结果
		kernel::log("taobao_api return:".$this->c->appkey.json_encode($resp));
		 kernel::log("taobao_api return:".$this->top_session);
	 
		$rsp = 'fail';
		$issucc = false;
		if($resp->code || $resp->msg){
			$res = 'code:'.$resp->code.', msg:'.$resp->msg.' ,submsg:'.$resp->submsg.',sub_code'.$resp->sub_code;

		}elseif(!$resp){
			$resp = ('超时');
		}else{
			$rsp = 'succ';
			$res = '成功';
			$issucc=true;
		}

	 

		$json='{
				"rsp": "'.$rsp.'",
				"msg_id":"3463421b2581604c74dcb9b74ffab2e4",
				"res":"'.$res.'",
				"data":{"shipping":{
		          "is_success": "'.$issucc.'"
		        }}
			}';
		//echo $json;
		return $json;

	}
	/*
	 * 同步库存
	* taobao.item.quantity.update
	*/
	function store_items_quantity_list_update($params){
		$req = new esb_rpc_request_taobao_top_request_ItemQuantityUpdateRequest();
		$rsp = 'fail';
		$true_bn=array();
		$error_bn=array();
		$db=kernel::database();
		
		$list_quantity=$params['list_quantity'];
		$list_quantity=json_decode($list_quantity,true);
		foreach ( $list_quantity as $k => $v ) {
			if($v['tsku']){
				
			// 取得对应的NumIid
			$numid = "";
			$sql="select taobao_num_iid from sdb_ome_goods g 
						left join sdb_ome_products p on g.goods_id=p.goods_id
						 where p.taobao_sku_id={$v['tsku']}";
			$numid = $db->selectrow ( $sql );
			$numid=$numid['taobao_num_iid'];
			
			if ($numid) {
				$req->setNumIid ( $numid );
				$req->setType ( 1 );
				$req->setSkuId( $v['tsku']);
				$req->getQuantity ( $v['quantity'] );
				$resp = $this->c->execute ( $req, $this->top_session );
				
				if ($resp->code || $resp->msg) {
					$res = ('code' . $resp->code . 'msg' . $resp->msg);
					$rsp = 'fail';
					$error_bn [] = array ("$v");
				} elseif (! $resp) {
					$rsp = 'fail';
					$resp = ('超时');
					$error_bn [] = array ("$v");
				} else {
					$rsp = 'succ';
					$res = '成功';
					$true_bn [] = array (	"$v");
				
				}
			}
		}
		}

		 
		$json='{
					"rsp": "'.$rsp.'",
					"res":"'.$res.'",
					"data": {
								"true_bn":'.$true_bn.',
								"error_bn":'.$error_bn.',
								"no_bn":[]
								}
					}';
		
		return $json;
	}

	/*
	 * 修改物流公司和运单号
	* taobao.logistics.consign.resend
	*/
	function store_trade_shipping_update($params){
		$req = new esb_rpc_request_taobao_top_request_LogisticsConsignResendRequest();

		$tid = $params['tid'];
		$closereason = $params['reason'];

		$req->setTid($tid);
		$req->setOutSid("123456789");
		$req->setCompanyCode("POST");
			
			
		$resp = $this->c->execute($req,$this->top_session);// 执行API请求并打印结果
		kernel::log("api return:".serialize($resp));
		if($resp->code || $resp->msg){
			$resp = ('【code】'.$resp->code.'<br/>【msg】'.$resp->msg);

		}elseif(!$resp){
			$resp = ('超时');
		}else{
			$resp = 'success';
		}

		return $json;

	}

	/*
	 * 卖家关闭交易接口
	*/
	function store_trade_status_update ($params){
		if($params['status']=='TRADE_CLOSED'){
			$req = new esb_rpc_request_taobao_top_request_TradeCloseRequest();

			$tid = $params['tid'];
			$closereason = $params['reason'];

			$req->setTid($tid);
			$req->setCloseReason($closereason);
				
				
			$resp = $this->c->execute($req,$this->top_session);// 执行API请求并打印结果
			kernel::log("taobao_api return:".serialize($resp));
			if($resp->code || $resp->msg){
				$resp = ('【code】'.$resp->code.'<br/>【msg】'.$resp->msg);

			}elseif(!$resp){
				$resp = ('超时');
			}else{
				$resp = 'success';
			}
		}else{
			
			
		}

		return $json;

	}


}